/**
 * @file:          CommunicationInterface.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.19
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.19,13:46:20
 */

/* Include Files **************************************************************/
#include "CommunicationInterface.h"
#include "string.h"

/* Global Variable Define *****************************************************/

/* Function Define ************************************************************/

uint8 CommunicationInterface_Init(struct CommunicationInterface *self)
{
    /* init function */
    // self->ReceiveDataPack = CommunicationInterface_ReceiveDataPack;
    // self->DataVerify = CommunicationInterface_DataVerify;
    // self->GetCommandArguments = CommunicationInterface_GetCommandArguments;
    self->CheckPassPrint = CommunicationInterface_CheckPassPrint;
    // self->CommandParsing = CommunicationInterface_CommandParsing;
    self->AddDataIntoSendArray = CommunicationInterface_AddDataIntoSendArray;
    // self->AddArrayIntoSendArray = CommunicationInterface_AddArrayIntoSendArray;

    /* init var */
    self->receive_data_pack.databyte = self->param_receive_buff_addr;

    return TRUE;
}

void CommunicationInterface_ReceiveDataPack(struct CommunicationInterface *self, uint8 receive_byte)
{
    //    // uint8 i;
    //    // DBprintf("c:%d\r\n", self->receive_count);
    //    // DBprintf("b:%#x\r\n", receive_byte);
    //    if (self->receive_count == 0)
    //    {
    //        if (receive_byte == self->param_data_frame_head_master)
    //        {
    //            self->receive_one_pack_ok = 0;
    //            self->receive_count++;
    //
    //            // DBprintf("0:\r\n");
    //        }
    //    }
    //    else if (self->receive_count == 1)
    //    {
    //        self->receive_data_pack.pack_type = receive_byte;
    //        self->receive_count++;
    //        // DBprintf("1:\r\n");
    //    }
    //    else if (self->receive_count == 2)
    //    {
    //        if (receive_byte > MAX_RECEIVE_BUFF)
    //        {
    //            self->receive_count = 0;
    //        }
    //        else
    //        {
    //            self->receive_data_pack.data_len = receive_byte;
    //            self->receive_count++;
    //        }
    //        // DBprintf("1:\r\n");
    //    }
    //    else if (self->receive_count >= 3 && self->receive_count < self->receive_data_pack.data_len + 3)
    //    {
    //        self->receive_data_pack.databyte[self->receive_count - 3] = receive_byte;
    //        // DBprintf("2:%#x\r\n", self->receive_data_pack.databyte[self->receive_count - 3]);
    //        self->receive_count++;
    //    }
    //    else if (self->receive_count == self->receive_data_pack.data_len + 3)
    //    {
    //        self->receive_data_pack.parity = CommunicationInterface_DataVerify(&self->receive_data_pack);
    //        if (receive_byte == self->receive_data_pack.parity)
    //        {
    //            self->receive_count++;
    //            // DBprintf("3y:%#x %#x\r\n", self->receive_data_pack.parity, receive_byte);
    //        }
    //        else
    //        {
    //            // DBprintf("3n:%#x %#x\r\n", self->receive_data_pack.parity, receive_byte);
    //            self->receive_count = 0;
    //        }
    //    }
    //    else if (self->receive_count == self->receive_data_pack.data_len + 4)
    //    {
    //        if (receive_byte == self->param_data_frame_head_slave)
    //        {
    //            self->receive_one_pack_ok = 1;
    //            // DBprintf("4:");
    //            // for (i = 0; i < self->receive_data_pack.data_len; i++)
    //            // {
    //            //     DBprintf("%#x ", self->receive_data_pack.databyte[i]);
    //            // }
    //        }
    //        self->receive_count = 0;
    //    }
    //    else
    //    {
    //        self->receive_count = 0;
    //    }
}

uint8 CommunicationInterface_DataVerify(DataRaw *data_pack)
{
    uint8 verity_sum = 0;
    uint8 i = 0;

    verity_sum += data_pack->pack_type;
    for (i = 0; i < data_pack->data_len; i++)
    {
        verity_sum += data_pack->databyte[i];
    }

    return verity_sum;
}

uint8 CommunicationInterface_GetCommandArguments(uint8 args_str_len, uint8 *args_str, uint8 *args_list)
{
    uint8 args_num = 0;

    return args_num;
}

void CommunicationInterface_CheckPassPrint(uint8 value, uint8_t print_en, char *pass_str, char *fail_str)
{
    if (print_en && value)
    {
        // TERMINAL(pass_str);
    }
    else if (print_en)
    {
        // TERMINAL(fail_str);
    }
}

void CommunicationInterface_CommandParsing(int argc, char const *argv[])
{
    // DBprintf("---\r\n");
    // DBprintf("%d\r\n", argc);
    // DBprintf("%s\r\n", argv[0]);
    // DBprintf("%s\r\n", argv[1]);
    // DBprintf("%s\r\n", argv[2]);
}

void CommunicationInterface_AddDataIntoSendArray(struct CommunicationInterface *self, uint8_t data)
{
    memcpy(&self->send_data_array[self->send_data_array_len], &data, 1);
    self->send_data_array_len += 1;
}

void CommunicationInterface_AddArrayIntoSendArray(struct CommunicationInterface *self, uint8_t *data, uint16_t len)
{
    memcpy(&self->send_data_array[self->send_data_array_len], data, len);
    self->send_data_array_len += len;
}